<?php
    $action=$_GET['action'];
	$licesneID=$_GET['licesneID'];
	$type=$_GET['type'];

	$updateID=($_GET['updateID'])*100;
	if($updateID<100){
	  $updateID=100;
	}
	//将updateID*100就可以和以前的一模一样了
	$relmove=$updateID+1;
	$connect=mysql_connect("localhost","root","");
    mysql_select_db("shenji",$connect);
    mysql_query("set names utf8");
	//根据type找到type表中的beizhu(英文找到中文)
	$sql="select * from type where ename='$type'";
	$result=mysql_query($sql);
	$row=mysql_fetch_array($result);
    $type=$row['name'];
    
    $manage=$row['manage'];
	$typeid=$row['typeid'];
	$array=array();
	if($licesneID!=null&&$manage==0){
		$sqlj="select * from jiekou where licesneid='".$licesneID."'";
		$resultj=mysql_query($sqlj);
		//$rowj=mysql_fetch_assoc($resultj);
		$data=mysql_num_rows($resultj);
		echo $data;exit;
		if($data){
			//当已存在机器码时，将机器码存入到other_jiekou表中
			$ip=$_SERVER["REMOTE_ADDR"];
			$beizhu=$licesneID.$ip;
			//$sql="insert into other_jiekou(licesneid,beizhu,typeid) value('$licesneID','$beizhu','$typeid')";
		   //	mysql_query($sql);
			$array["status"]=1;
			$array["xingxi"]=urlencode("licesneID现在已存入other_jiekou表");
		}else{
			//当不存在机器码时，将机器码存入到jiekou表中
		   $ip=$_SERVER["REMOTE_ADDR"];
		   $beizhu=$licesneID.$ip;
		   $sql="insert into jiekou(licesneid,beizhu,status,typeid) value('$licesneID','$beizhu','1','$typeid')";
		   mysql_query($sql);
		   $array["status"]=1;
		   $array["xingxi"]=urlencode("licesneID现在已存入jiekou表");
		}

	}elseif($licesneID!=null&&$manage==1){
			
		$sqlj="select * from jiekou where licesneid='".$licesneID."'";
		$resultj=mysql_query($sqlj);
		$data=mysql_num_rows($resultj);
		if(!$data){
		   $ip=$_SERVER["REMOTE_ADDR"];
		   $beizhu=$licesneID.$ip;
		   $sql="insert into jiekou(licesneid,beizhu,status,typeid) value('$licesneID','$beizhu','1','$typeid')";
		   mysql_query($sql);
		   $array["status"]=-1;
		   $array["xingxi"]=urlencode("licesneID现在已存入jiekou表");
		   $json=json_encode($array);
		   echo urldecode($json);	
		}else{
		   $array["status"]=1;
		}
			   
			
	}else{
	   $array["status"]=-1;
	   $array["xingxi"]=urlencode("请输入licesneID的值");
	   $json=json_encode($array);
	   echo urldecode($json);		   	
	}
		   	

	if($action==read){
		
		$sql="select * from jiekou where licesneid='".$licesneID."'";
	    $result=mysql_query($sql);
		$row=mysql_fetch_array($result);
		$jid=$row['id'];
		//echo $jid;exit;
		$status=$row['status'];
		if(isset($row['id'])){
	        $sql="select * from shenji where move=$relmove and type='".$type."'";
	        $result=mysql_query($sql);
	        $row=mysql_fetch_array($result);

	        $version=$row['version'];
		    $id=$row['id'];
		    $publish_jiqi=$row['publish_jiqi'];#开关
		    $jiekou_id=$row['jiekou_id'];
		    //echo $publish_jiqi;exit;
		    $sql2="select max(move) from shenji where type='".$type."'";
			$result2=mysql_query($sql2);
	        $row2=mysql_fetch_array($result2);
	        //print_r($row2);die;
			$max_move=$row2[0];
//			echo $max_move;exit;
			if($updateID<$max_move){	 
			   	$txt = sprintf("%1\$.2f",$version*0.01);
			   	$file_name=$txt."_update.tar.gz";
               	$file_dir="/var/www/html/upload/upload/".$type."/";
			    if (! file_exists ( $file_dir . $file_name )){    
				   echo "文件不存在";
				   exit ();    
                }else{
					$flag=0;
					 //publish:1:关闭  0:开启   status:1:开启  0:关闭     
    				 
					if($publish_jiqi==1){
					 	if($jiekou_id==$jid){  //关闭状态只有是本身才能下载
					     	if($status==1){    //本身要是开启状态
							 	$flag=1;
						 	}else{
						 		$flag=0;
						 	}
					 	}else{
					 		$flag=0;
					 	}
					 
				 	}else{                      //开启状态
					 	if($status==1){         //只要该机器码开启就行了
						 	$flag=1;
					 	}
				 	}
					 
					 
				 	if($flag){
						  //$size=filesize($file_dir.$file_name);
					/* 	  $file = fopen ( $file_dir . $file_name, "r" );    
						  Header ( "Content-type: application/octet-stream" );    
						  Header ( "Accept-Ranges: bytes" );    
						  Header ( "Accept-Length: " . filesize ( $file_dir . $file_name ) );    
						  Header ( "Content-Disposition: attachment; filename=$file_name" );         
						  echo fread ( $file, filesize ( $file_dir . $file_name ) );    
						  fclose ( $file );    
						  exit (); */
					 	header('content-type:application/octet-stream');
						header('accept-ranges: bytes');
						header('content-length: '.filesize($file_dir.$file_name));
						//header('Content-length: ' . $size);
						header('content-disposition:attachment;filename='.$file_name);
						$fp = fopen($file_dir.$file_name, "r");
						while(!feof($fp)) {
							echo fgets($fp,4096);
						}
						fclose($fp);
						   
				 	}else{
						//echo  
						 //header('HTTP/1.1 403 Forbidden');
		                // header("status: 403 Forbidden");
						 $array["status"]=-1;
						 $array["xingxi"]=urlencode("权限被禁止!");
						 $json=json_encode($array);
						 //echo urldecode($json);
						 //echo $json;
				 	}
					 
			 	}
				 
	 		}/*else{
				
				  header('http/1.1 404 Not Found');
                  header("status: 404 Not Found"); 
				  $array["status"]=-1;
				  $array["xingxi"]=urlencode("没有更新的版本或类型输入错误!");
				  $json=json_encode($array);
				  echo urldecode($json);
		  	
			
		  
				  
		 	}*/
			 
	   	}else{
		   header('HTTP/1.1 403 Forbidden');
		   header("status: 403 Forbidden");
		   /*if($licesneID!=null||$manage==0){
			   //将licesneID添加数据库
			   $ip=$_SERVER["REMOTE_ADDR"];
			   $beizhu=$licesneID.$ip;
			   $sql="insert into other_jiekou(licesneid,beizhu,manage) value('$licesneID','$beizhu','$manage')";
			   mysql_query($sql);
			   $array["status"]=-1;
			   $array["xingxi"]=urlencode("licesneID现在已存入");
			   $json=json_encode($array);
			   echo urldecode($json);
		   }elseif($licesneID!=null||$manage==1){
		   	   $ip=$_SERVER["REMOTE_ADDR"];
			   $beizhu=$licesneID.$ip;
			   $sql="insert into jiekou(licesneid,beizhu,status,manage) value('$licesneID','$beizhu','1','$manage')";
			   mysql_query($sql);
			   $array["status"]=-1;
			   $array["xingxi"]=urlencode("licesneID现在已存入");
			   $json=json_encode($array);
			   echo urldecode($json);
		   }else{
			   $array["status"]=-1;
			   $array["xingxi"]=urlencode("请输入licesneID的值");
			   $json=json_encode($array);
			   echo urldecode($json);
			   
		   }*/
		    
		   	

	   	}
    }else{
	   header('HTTP/1.1 403 Forbidden');
	   header("status: 403 Forbidden");
	   $array["status"]=-1;
	   $array["xingxi"]=urlencode("输入的action值有误!");
	   $json=json_encode($array);
	   echo urldecode($json);
    }
?>